package com.drivers.user.controller;

import cn.hutool.core.util.RandomUtil;
import com.drivers.common.Sample;
import com.drivers.model.Result;
import com.drivers.model.ResultVo;
import com.drivers.model.user.CountMoney;
import com.drivers.model.user.FindId;
import com.drivers.model.user.User;
import com.drivers.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/findByidUser/{id}")
    public User findUser(@PathVariable("id")Integer id){
        return userService.findUser(id);
    }

    @RequestMapping("/findCountMoney/{id}")
    public List<CountMoney> findCountMoney(@PathVariable("id")Integer id){
        return userService.findCountMoney(id);
    }

    @RequestMapping("/findByIdCountMoney")
    public CountMoney findByIdCountMoney(@RequestBody FindId id){
        return userService.findByIdCountMoney(id);
    }

    @RequestMapping("/updatePoint/{driverPoint}/{id}")
    public void updatePoint(@PathVariable("driverPoint")Integer driverPoint,@PathVariable("id")Integer id){
        userService.updatePoint(driverPoint,id);
    }
    @RequestMapping("/updateAssetsQuan/{id}")
    public ResultVo updateAssetsQuan(@PathVariable("id")Integer id){
        int i= userService.updateAssetsQuan(id);
        return ResultVo.ok(i);
    }
    @RequestMapping("/updateAssetsId/{Assets}/{id}")
    public ResultVo updateAssetsId(@PathVariable("Assets")Integer assets,@PathVariable("id")Integer id){
        int i= userService.updateAssetsId(assets,id);
        return ResultVo.ok(i);
    }
    @RequestMapping("/loginName/{username}")
    public ResultVo loginName(@PathVariable("username")String username){
        User user= userService.loginName(username);
        return ResultVo.ok(user);
    }
    @RequestMapping("/loginPassword/{id}/{password}")
    public ResultVo loginPassword(@PathVariable("id")Integer id,@PathVariable("password")String password){
        int i= userService.loginPassword(id,password);
        return ResultVo.ok(i);
    }
    @RequestMapping("/userRegister")
    public void userRegister(@RequestBody User user){
         userService.userRegister(user);
    }

    @RequestMapping("/findPhone/{phone}")
    public ResultVo findPhone(@PathVariable("phone") String phone){
        return ResultVo.ok(userService.findPhone(phone));
    }


    @RequestMapping("/findName/{driverName}")
    public ResultVo findName(@PathVariable("driverName") String driverName){
        return ResultVo.ok(userService.findName(driverName));
    }

    @RequestMapping("message/{phone}")
    public Result Login(@PathVariable("phone") String phone) throws Exception {

        if (phone==null){
            return Result.error("账号不能为空");
        }
        //获取随机数
        String s = RandomUtil.randomNumbers(4);

        //调用阿里云服务
        Sample.sendSms(phone,s);
        //把验证码存入redis中 设置有效期为二分钟
        redisTemplate.opsForValue().set("login"+phone,s,2, TimeUnit.MINUTES);

        log.info("验证码:{}",s);

        return Result.success(s);
    }
}
